package Java实训;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class 实训成果物 {
    public static void main(String[] args) throws IOException, ParseException {
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\jobs_new.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result.txt"));
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("                  数据处理");
            System.out.println("-----------------------------------------------");
            System.out.println(" 11、去除数据表中冗余数据（岗位、公司名称、区域一样）");
            System.out.println("     12、将薪资列规范为月薪XX元存储成浮点型数据");
            System.out.println("13、归一化发布时间和采集时间，形成年月日时分秒格式的时间结构");
            System.out.println("                0、退出");
            System.out.println("-----------------------------------------------");
            System.out.println("                  数据统计");
            System.out.println("-----------------------------------------------");
            System.out.println(" 1、统计不同城市昨天、本周、本月发布岗位数量");
            System.out.println("       2、统计不同城市平均薪资");
            System.out.println("     3、统计不同学历的平均薪资");
            System.out.println("              0、退出");
            System.out.println("-----------------------------------------------");
            System.out.println("请输入您要进行的操作:");
            int a = sc.nextInt();
            if (a == 0) {
                break;
            } else if (a == 1) {
                BufferedReader br1 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\jobs_new.txt"));
                BufferedWriter bw1 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedReader br2 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedWriter bw2 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedReader br3 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedWriter bw3 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result.txt"));
                deal1(br1, bw1);
                br1.close();
                bw1.close();
                deal3(br2, bw2);
                br2.close();
                bw2.close();
                count1(br3, bw3);
                br3.close();
                bw3.close();
            } else if (a == 2) {
                BufferedReader br1 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\jobs_new.txt"));
                BufferedWriter bw1 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedReader br2 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedWriter bw2 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedReader br3 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedWriter bw3 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result.txt"));
                deal1(br1, bw1);
                br1.close();
                bw1.close();
                deal2(br2, bw2);
                br2.close();
                bw2.close();
                count2(br3, bw3);
                br3.close();
                bw3.close();
            }else if (a == 3) {
                BufferedReader br1 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\jobs_new.txt"));
                BufferedWriter bw1 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedReader br2 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result1.txt"));
                BufferedWriter bw2 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedReader br3 = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result2.txt"));
                BufferedWriter bw3 = new BufferedWriter(new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result.txt"));
                deal1(br1, bw1);
                br1.close();
                bw1.close();
                deal2(br2, bw2);
                br2.close();
                bw2.close();
                count7(br3, bw3);
                br3.close();
                bw3.close();
            } else if (a == 11) {
                deal1(br, bw);
                br.close();
                bw.close();
            } else if (a == 12) {
                deal2(br, bw);
                br.close();
                bw.close();
            } else if (a == 13) {
                deal3(br, bw);
                br.close();
                bw.close();
            } else {
                System.out.println("您输入的格式不正确，请重新输入");
                continue;
            }
        }
    }

    //1、去除数据表中冗余数据（岗位、公司名称、区域一样）
    public static void deal1(BufferedReader br, BufferedWriter bw) throws IOException {
        Set<String> set = new HashSet<>();
        String str;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            if (set.contains(s[1] + s[5] + s[7])) {
                continue;
            } else {
                bw.write(str + "\n");
            }
            set.add(s[1] + s[5] + s[7]);
        }
    }

    //5、将薪资列规范为月薪XX元（数据10万以上按照年薪进行处理），存储成浮点型数据(面议数据删除)
    public static void deal2(BufferedReader br, BufferedWriter bw) throws IOException {
        String str;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            if (s[4].contains("元以上")) {
                s[4] = s[4].replace("元以上", "");
                double xz = Double.parseDouble(s[4]);
                if (xz > 100000) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz + "元";
                }

            } else if (s[4].contains("万/月")) {
                s[4] = s[4].replace("万/月", "");
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1])) / 2;
                if (xz > 10) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz * 10000 + "元";
                }

            } else if (s[4].contains("面议")) {
                continue;
            } else if (s[4].contains("薪") && s[4].contains("k")) {
                s[4] = s[4].replace("·", "");
                s[4] = s[4].replace("薪", "");
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1].split("k")[0])) / 2;
                s[4] = "年薪" + xz * 1000 * Double.parseDouble(s[4].split("-")[1].split("k")[1]) + "元";
            } else if (s[4].contains("K")) {
                s[4] = s[4].replace("K", "");
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1])) / 2;
                if (xz > 100) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz * 1000 + "元";
                }

            } else if (s[4].contains("k")) {
                s[4] = s[4].replace("k", "");
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1])) / 2;
                if (xz > 100) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz * 1000 + "元";
                }

            } else if (s[4].contains("元")) {
                s[4] = s[4].replace("元", "");
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1])) / 2;
                if (xz > 100000) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz + "元";
                }

            } else {
                double xz = (Double.parseDouble(s[4].split("-")[0]) + Double.parseDouble(s[4].split("-")[1])) / 2;

                if (xz > 100000) {
                    s[4] = "年薪" + xz * 12 + "元";
                } else {
                    s[4] = "月薪" + xz + "元";
                }

            }
            for (int i = 0; i < s.length; i++) {
                if (i == 0) {
                    bw.write(s[i]);
                } else {
                    bw.write("," + s[i]);
                }
            }
            bw.write("\n");
        }
    }

    //7、归一化发布时间和采集时间，形成年月日时分秒格式的时间结构
    public static void deal3(BufferedReader br, BufferedWriter bw) throws IOException, ParseException {
        String str;
        while ((str = br.readLine()) != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String[] s = str.split(",");
            if (s[8].contains("小时前")) {
                Date d1 = sdf.parse(s[9]);
                Date d2 = new Date(d1.getTime() - Integer.parseInt(s[8].replace("小时前", "")) * 60 * 60 * 1000);
                s[8] = sdf.format(d2);
            } else if (s[8].contains("天前")) {
                Date d1 = sdf.parse(s[9]);
                Date d2 = new Date(d1.getTime() - Integer.parseInt(s[8].replace("天前", "")) * 24 * 60 * 60 * 1000);
                s[8] = sdf.format(d2);
            }
            for (int i = 0; i < s.length; i++) {
                if (i == 0) {
                    bw.write(s[i]);
                } else {
                    bw.write("," + s[i]);
                }
            }
            bw.write("\n");
        }
    }

    //1、统计不同城市昨天、本周、本月发布岗位数量
    public static void count1(BufferedReader br, BufferedWriter bw) throws IOException, ParseException {
        String str;
        int y1 = 0, y2 = 0, y3 = 0;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1 = sdf.parse(s[9]);
            Date d2 = sdf.parse(s[8]);
            if ((d1.getTime() - d2.getTime()) < (24 * 60 * 60 * 1000)) {
                y1++;
            } else if ((d1.getTime() - d2.getTime()) < (24 * 60 * 60 * 1000 * 7)) {
                y2++;
            } else if ((d1.getTime() - d2.getTime()) < (24 * 60 * 60 * 1000 * 30)) {
                y3++;
            }
        }
        bw.write("昨天岗位数量: " + (y1) + "\n");
        bw.write("本周岗位数量: " + (y1 + y2) + "\n");
        bw.write("本月岗位数量: " + (y1 + y2 + y3) + "\n");
    }

    //2、统计不同城市平均薪资
    public static void count2(BufferedReader br, BufferedWriter bw) throws IOException {
        HashMap<String, Double> map = new HashMap<>();
        String str;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            if (map.containsKey(s[7])) {
                double value = map.get(s[7]);
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    value = (value + xz / 12) / 2;
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    value = (value + xz) / 2;
                }
                map.put(s[7], value);
            } else {
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    map.put(s[7], xz / 12);
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    map.put(s[7], xz);
                }
            }
        }
        Set<Map.Entry<String, Double>> set = map.entrySet();
        for (Map.Entry<String, Double> stringIntegerEntry : set) {
            bw.write(stringIntegerEntry.getKey() + ": " + stringIntegerEntry.getValue() + "\n");
        }
    }

    //4、统计全国不同工作经验的平均薪资
    public static void count4(BufferedReader br, BufferedWriter bw) throws IOException {
        HashMap<String, Double> map = new HashMap<>();
        String str;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            if (map.containsKey(s[2])) {
                double value = map.get(s[2]);
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    value = (value + xz / 12) / 2;
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    value = (value + xz) / 2;
                }
                map.put(s[2], value);
            } else {
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    map.put(s[2], xz / 12);
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    map.put(s[2], xz);
                }
            }
        }
        Set<Map.Entry<String, Double>> set = map.entrySet();
        for (Map.Entry<String, Double> stringIntegerEntry : set) {
            bw.write(stringIntegerEntry.getKey() + ": " + stringIntegerEntry.getValue() + "\n");
        }
    }

    //7、统计不同学历的平均薪资
    public static void count7(BufferedReader br, BufferedWriter bw) throws IOException {
        HashMap<String, Double> map = new HashMap<>();
        String str;
        while ((str = br.readLine()) != null) {
            String[] s = str.split(",");
            if (map.containsKey(s[3])) {
                double value = map.get(s[3]);
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    value = (value + xz / 12) / 2;
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    value = (value + xz) / 2;
                }
                map.put(s[3], value);
            } else {
                if (s[4].contains("年薪")) {
                    double xz = Double.parseDouble(s[4].replace("年薪", "").replace("元", ""));
                    map.put(s[3], xz / 12);
                } else {
                    double xz = Double.parseDouble(s[4].replace("月薪", "").replace("元", ""));
                    map.put(s[3], xz);
                }
            }
        }
        Set<Map.Entry<String, Double>> set = map.entrySet();
        for (Map.Entry<String, Double> stringIntegerEntry : set) {
            bw.write(stringIntegerEntry.getKey() + ": " + stringIntegerEntry.getValue() + "\n");
        }
    }

}